package com.coder520.common.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2017-7-8.
 */
public class SecurityUtils {

    public static String encryptPassword(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
//        解决可能乱码问题
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String encryptpassword = base64Encoder.encode(md5.digest(password.getBytes("UTF-8")));

        return encryptpassword;
    }

    public static boolean checkPassword(String inputpwd,String dbpwd) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        if (dbpwd.equals(encryptPassword(inputpwd))) {
            return true;
        }
        else return false;
    }
}
